/*
 * UMLSynch plugin for Eclipse
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.eclipse.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

import com.googlecode.umlsynch.EclipseDNDWrapper.EclipseKeysListener;
import com.googlecode.umlsynch.EclipseDNDWrapper.EclipsePartListener;
import com.googlecode.umlsynch.eclipse.rpc.ClientFactory;
import com.googlecode.umlsynch.eclipse.rpc.Server;
import com.googlecode.umlsynch.eclipse.rpc.Debug.TEclipseDebug;
import com.googlecode.umlsynch.eclipse.rpc.dialogs.RmiConfigDialog;
import com.googlecode.umlsynch.rpc.TClassDescriptor;
import com.googlecode.umlsynch.rpc.TFileDescriptor;
import com.googlecode.umlsynch.rpc.IIndexerRpc.TRelationRpc;
import com.googlecode.umlsynch.rpc.base.TConfig;
import com.googlecode.umlsynch.rpc.base.TConfigReader;


/**
 * Our sample action implements workbench action delegate.
 * The action proxy will be created by the workbench and
 * shown in the UI. When the user tries to use the action,
 * this delegate will be created and execution will be 
 * delegated to it.
 * @see IWorkbenchWindowActionDelegate
 */
public class UMLSynchStartAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;
	//private TEclipseOperationServerDispatcher eclipseOperationServer = null;
	//private static TEclipseOperationServerImplementation2 imp2;
	private Server eclipseOperationServer = null;
	private EclipseKeysListener keyListener = null;
	/**
	 * The constructor.
	 */
	public UMLSynchStartAction() {
	}

	/**
	 * The action has been activated. The argument of the
	 * method represents the 'real' action sitting
	 * in the workbench UI.
	 * @see IWorkbenchWindowActionDelegate#run
	 */
	public void run(IAction action) {

		if (eclipseOperationServer == null)
		{
			// TODO: HOT KEYs SUPPORT 
			// keyListener = new EclipseKeysListener();
			// keyListener.RegistListener();

			// Old RPC interfaces
			// imp2 = new TEclipseOperationServerImplementation2();
			//eclipseOperationServer  = new TEclipseOperationServerDispatcher(imp2);			
			
			// New RPC interfaces:
			// Open RPC config dialog, the next step is to add it 
			// into the Eclipse configs
			
			TConfigReader cfgreader = 
				new TConfigReader("C:/Temp/umlsynch.ini", new TEclipseDebug("ConfigReader"));

			RmiConfigDialog dialog = new RmiConfigDialog(window.getShell());
			if (cfgreader.getReadedCount() > 0) {
				TConfig c = cfgreader.getConfig("EclipseIndexer");
				if (c != null)
				  dialog.eclipse = c ;

				c = cfgreader.getConfig("VPSDEEDITOR");
				if (c != null)
				  dialog.vpsde = c;
			}
			dialog.open();
			
			if (dialog.eclipse != null) {
				eclipseOperationServer = new Server(dialog.eclipse, new TEclipseDebug("Server"));
			}
			if (dialog.vpsde != null) {
				// It is necessary to provide configs for
				// clients
				new ClientFactory(dialog.vpsde, new TEclipseDebug("Client"));
			}
			
			// DND support:
			// EclipsePartListener is responsible for 
			// adding resource transfer support for 
			// "Project Explorer", "C/C++ Projects" viewes
			IWorkbenchPage[] wbPages = window.getPages();
			for (int i = 0 ; i<wbPages.length; ++i) {
				wbPages[i].addPartListener(new EclipsePartListener());
			} 
		}else {
			TClassDescriptor desc = new TClassDescriptor();
			desc.file = new TFileDescriptor();
			desc.name = "testClass";
			desc.language = "C/C++";
			desc.file.project = "testProject";
			eclipseOperationServer.erpc.OpenClass(desc );
			eclipseOperationServer.frpc.FindClassByName(null, "testClass");
			desc = eclipseOperationServer.irpc.GetClassInfo(desc);
			eclipseOperationServer.irpc.GetClassRelations(desc, TRelationRpc.CLASS_GENERALIZATION, null);
		}
	}

	/**
	 * Selection in the workbench has been changed. We 
	 * can change the state of the 'real' action here
	 * if we want, but this can only happen after 
	 * the delegate has been created.
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

	/**
	 * We can use this method to dispose of any system
	 * resources we previously allocated.
	 * @see IWorkbenchWindowActionDelegate#dispose
	 */
	public void dispose() {
		
	}

	/**
	 * We will cache window object in order to
	 * be able to provide parent shell for the message dialog.
	 * @see IWorkbenchWindowActionDelegate#init
	 */
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
}